<?php

namespace app\common\controller;

use think\Request;
class Api extends Common
{
    //需要用户id的表单
    protected $cz = ['Bankcard','Wallet','Order','Address'];
    //需要所有数据的表单-没分页
    protected $all = ['Classs','Loansetting'];
    //有关联需求的表单
    protected $with = ['Product'];
    //关联的方法名
    protected $withaction = ['Product'=>'atlas'];
    // //appID
    protected $appid = 'wxedf0be8a1de0b598';
    // //app秘钥
    protected $appsecret = 'dd3697c218935dad84b79fd0aa43ef14';
    //商户号
    protected $mchid = '1559100501';
    //api秘钥
    protected $mchkey = '57Wc8DGqNwCkzjyQbgnWxdHLPRnfrp64';
	//Mqtt 配置
	protected $server = "mqtt.langqiyun.cn";    		// 服务代理地址(mqtt服务端地址)
	protected $port = 1883;                     		// 通信端口
	protected $username = "langqi";                 	// 用户名(如果需要)
	protected $password = "lq20171201";                 // 密码(如果需要
	protected $client_id = "a123";						// 设置你的连接客户端id（这里随便写个，实际使用是用的主题来绑定到每一台机子上的）
    //列表数据
    public function index()
    {
        if($this->request->isPost())
        {
            $post = input('post.');
            $post = array_filter($post);
            $limit = $post['limit'] ?? 10;
            $page = $post['page'] ?? 1;
            if(in_array($this->request->controller(),$this->cz)){
                if(isset($post['user_id'])){
                    try{
                        $list = model($this->request->controller())->where('user_id','=',$post['user_id'])->paginate($limit);
                    }catch (\Exception $e){
                        return fh(500,'error',$e->getMessage(),[]);
                    }
                }else{
                    return fh('404','error','没有用户id',[]);
                }
            }elseif(in_array($this->request->controller(),$this->all)){
                try{
                    $list = model($this->request->controller())->select()->toArray();
                }catch (\Exception $e){
                    return fh(500,'error',$e->getMessage(),[]);
                }
            }else{
                try{
                    $list = model($this->request->controller())->paginate($limit);
                }catch (\Exception $e){
                    return fh(500,'error',$e->getMessage(),[]);
                }
            }
            return fh(200,'success','成功',$list);

        }
    }
    //数据添加
    public function add()
    {
        if($this->request->isPost())
        {
            $post = input('post.');
            if ($post){
                try{
                    $model = model($this->request->controller())->save($post);
                }catch (\Exception $e){
                    return fh(500,'error',$e->getMessage(),[]);
                }
                return fh(200,'success','添加成功',$model);
            }else{
                return fh(404,'error','没有post数据',[]);
            }
        }
    }
    //数据删除
    public function delete()
    {
        if($this->request->isPost())
        {
            $post = input('post.');
            if ($post){
                try{
                    $model = model($this->request->controller())->destroy($post['id']);
                }catch (\Exception $e){
                    return fh(500,'error',$e->getMessage(),[]);
                }
                return fh(200,'success','删除成功',$model);
            }else{
                return fh(404,'error','没有post数据',[]);
            }
        }
    }
    //数据修改
    public function updete()
    {
        if($this->request->isPost())
        {
            $post = input('post.');
            try{
                $id = $post['id'];
                $post['id'] = '';
                $post = array_filter($post);
                if($post){
                	$model = model('order')->save($post,['id'=>$id]);
                }else{
                	return fh(404,'error','post数据为空',[]);
                }
            }catch (\Exception $e){
                return fh(500,'error',$e->getMessage(),[]);
            }
            return fh(200,'success','添加成功',$model);
        }
    }
    //详细数据
    public function view()
    {
        if($this->request->isPost())
        {
            $post = input('post.');
            if(!isset($post['id'])){
                return fh(404,'error','没有id',[]);
            }
            try{
                if(in_array($this->request->controller(),$this->with))
                {
                    $model = model($this->request->controller())->with($this->withaction[$this->request->controller()])
                        ->where('id','=',$post['id'])->find();
                    if($model){
                    	$model = $model->toArray();
                    }
                }else{
                    $model = model($this->request->controller())->get($post['id']);
                    if($model){
                    	$model = $model->toArray();
                    }
                }
                if(empty($model)){
            		return fh(404,'error','该id没有查询到数据',[]);
                }
            }catch (\Exception $e){
                return fh(500,'error',$e->getMessage(),[]);
            }
            if(isset($model['content'])){
            	$model['content'] = preg_replace('/(<img.+?src=")(.*?)/', '<img src="' . 'https://baosheng.langqiyun.cn', $model['content']);
            }
            return fh(200,'success','请求成功',$model);
        }
    }
}
